package com.jy.modules.cims.component.image.task;

import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.component.image.TransferImagesDto;
import com.jy.modules.cims.component.image.TransferImagesHelper;

/**
 * @description 公共抽象的处理影像任务
 * @author shengzhoutao
 * @date 2015-09-29
 */
public abstract class AbstractTransferImagesTask implements Runnable {
	private static Logger logger = LoggerFactory.getLogger(AbstractTransferImagesTask.class);

	public final int WAIT_QUEUE_SIZE = 10;// 队列一次放入数据个数
	public final int EMPTY_QUEUE_SLEEP_MILLIS = 1000 * 1;
	

	@Autowired
	@Qualifier("com.jy.modules.cims.component.image.TransferImagesHelper")
	private TransferImagesHelper transferImagesHelper;

	/**
	 * @description 线程执行run方法
	 */
	@Override
	public void run() {
		while (true) {
			Boolean isMoreThread = transferImagesHelper.isMoreThread();
			if (isMoreThread) {
				//Thread.currentThread().setName(this.getClass().getName());
				//logger.debug("当前线程名:::" + Thread.currentThread().getName());
				
				doSomething();
			} else {
				sleep(1000 * 60 * 10);
			}
			sleep(500);
		}
	}

	/**
	 * @description 任务业务处理方法
	 */
	public abstract void doSomething();

	/**
	 * @description 暂停sleep
	 * @param millis
	 */
	public void sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			logger.error(e.getMessage());
		}
	}

	/**
	 * @description 从队列取处理影像 getTransferImagesByQueue
	 * @param queue
	 * @return
	 */
	public TransferImagesDto getTransferImagesByQueue(LinkedBlockingQueue<TransferImagesDto> queue) {
		TransferImagesDto transferImagesDto = queue.poll();
		return transferImagesDto;
	}
	
	
	public void notifyQuery() {
		CimsConstant.mmediatelyQuery = true;
	}
	
	public void suspendQuery() {
		CimsConstant.mmediatelyQuery = false;
	}

}
